/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.qb9.flashlib.tasks
{
	/**
	 * Equals to flash's setTimeout. Executes a function after a certain delay.
	 * 
	 * <p>To create an interval, use a Timeout inside a Loop.</p>
	 * 
	 * @see Loop
	 */
	
	public class Timeout extends Wait
	{
		private var callback:Function;
		private var args:Array;
		
		public function Timeout(callback:Function, delay:uint, ...args)
		{
			super(delay);
			
			this.callback = callback;
			this.args = args;
		}
		
		/** @inheritDoc */
		protected override function taskComplete():void
		{
			callback.apply(null, args);
			super.taskComplete();
		}
		
		/**
		 * Utility function to create a Timeout task from an arguments array.
		 *
		 * @param callback The callback function.
		 * @param delay The callback function.
		 * @param args The arguments that will be passed to the callback function.
		 */
		public static function fromArgsArray(callback:Function, delay:uint, args:Array):Timeout
		{
			var t:Timeout = new Timeout(callback, delay);
			t.args = args;
			return t;
		}
		
		/** @inheritDoc */
		public override function clone():ITask
		{
			return fromArgsArray(callback, duration, args);
		}
	}
}